//
//  ANUserLoginViewController.m
//  Ants
//
//  Created by ZHM on 16/8/6.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANUserLoginViewController.h"
#import "ANUserLogonViewController.h"
#import "ANUserForgetPassWordViewController.h"
#import "ANUserQuickLogonViewController.h"

#define Start_X 60.0f           // 第一个按钮的X坐标
#define Start_Y 440.0f           // 第一个按钮的Y坐标
#define Width_Space 50.0f        // 2个按钮之间的横间距
#define Height_Space 20.0f      // 竖间距
#define Button_Height 50.0f    // 高
#define Button_Width 50.0f      // 宽
#define ANLoginOKNote @"loginOKNote"

@interface ANUserLoginViewController ()<UIScrollViewDelegate>
{
    UIImageView *View;
    UIView *bgView;
    UITextField *pwd;
    UITextField *userPhone;
    UIButton *QQBtn;
    UIButton *weixinBtn;
    UIButton *xinlangBtn;
}
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIImageView *bgImageView;

@property(copy,nonatomic) NSString * accountNumber;
@property(copy,nonatomic) NSString * mmmm;

@end

@implementation ANUserLoginViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //self.hidesBottomBarWhenPushed = YES;
    //self.navigationController.navigationBarHidden = YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置背景颜色
    View = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [View setImage:[UIImage imageNamed:@"bg5"]];
    [self.view addSubview:View];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
    _scrollView.delegate = self;
    
    self.title = @"登录";

    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(logon)];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(quit)];
        
    self.navigationItem.rightBarButtonItem = right;
    self.navigationItem.leftBarButtonItem = left;
    
    [self createTextFields];
    [self createButtons];
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    //隐藏滚动指示器
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    //设置内容内边距
    self.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 20, 0);
}

#pragma mark-开始拖拽 代理方法
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    //隐藏键盘
    [self.view endEditing:YES];
}

-(void)createTextFields {
    CGRect frame=[UIScreen mainScreen].bounds;
    bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 85, frame.size.width - 20, 100)];
    bgView.layer.cornerRadius = 3.0;
    bgView.alpha = 0.7;
    bgView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:bgView];
    
    userPhone = [self createTextFielfFrame:CGRectMake(60, 10, 271, 30) font:[UIFont systemFontOfSize:14] placeholder:@"请输入您手机号码/账号/邮箱"];
    userPhone.keyboardType = UIKeyboardTypeDefault;
    userPhone.clearButtonMode = UITextFieldViewModeWhileEditing;
    userPhone.tag = 100;
    
    pwd = [self createTextFielfFrame:CGRectMake(60, 60, 271, 30) font:[UIFont systemFontOfSize:14]  placeholder:@"密码" ];
    pwd.clearButtonMode = UITextFieldViewModeWhileEditing;
    pwd.tag = 101;

    //密文样式
    pwd.secureTextEntry = YES;
    //pwd.keyboardType=UIKeyboardTypeNumberPad;
    
    UIImageView *userImageView=[self createImageViewFrame:CGRectMake(20, 10, 25, 25) imageName:@"ic_landing_nickname" color:nil];
    UIImageView *pwdImageView=[self createImageViewFrame:CGRectMake(20, 60, 25, 25) imageName:@"mm_normal" color:nil];
    UIImageView *line1=[self createImageViewFrame:CGRectMake(20, 50, bgView.frame.size.width-40, 1) imageName:nil color:[UIColor colorWithRed:180/255.0f green:180/255.0f blue:180/255.0f alpha:.3]];
    
    [bgView addSubview:userPhone];
    [bgView addSubview:pwd];
    
    [bgView addSubview:userImageView];
    [bgView addSubview:pwdImageView];
    [bgView addSubview:line1];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.tag == 100) {
        if (textField.text.length > 35) {
            textField.text = [textField.text substringToIndex:11];
        }
    } else if (textField.tag == 101) {
        if (textField.text.length > 35) {
            textField.text = [textField.text substringToIndex:7];
        }
    }
}

-(void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [userPhone resignFirstResponder];
    [pwd resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [userPhone resignFirstResponder];
    [pwd resignFirstResponder];
}

-(void)createButtons
{
    UIButton *landBtn = [self createButtonFrame:CGRectMake(10, 210, self.view.frame.size.width - 20, 37) backImageName:nil title:@"登录" titleColor:[UIColor whiteColor]  font:[UIFont systemFontOfSize:19] target:self action:@selector(landClick)];
    landBtn.backgroundColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    landBtn.layer.cornerRadius=5.0f;

    UIButton *newUserBtn = [self createButtonFrame:CGRectMake(5, CGRectGetMaxY(landBtn.frame) + 10, 70, 30) backImageName:nil title:@"快速注册" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(registration:)];
    //newUserBtn.backgroundColor=[UIColor lightGrayColor];
    
    UIButton *forgotPwdBtn = [self createButtonFrame:CGRectMake(self.view.frame.size.width - 75, CGRectGetMaxY(landBtn.frame) + 10, 60, 30) backImageName:nil title:@"找回密码" titleColor:[UIColor grayColor] font:[UIFont systemFontOfSize:13] target:self action:@selector(fogetPwd:)];
    //fogotPwdBtn.backgroundColor=[UIColor lightGrayColor];
    
    CGFloat labelY = (self.view.height - CGRectGetMaxY(forgotPwdBtn.frame) - 91)/2 + CGRectGetMaxY(forgotPwdBtn.frame) - 44;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((self.view.width - 140)/2, labelY, 140, 21)];
    ANLog(@"%f --%f",labelY,self.view.height);
    label.text = @"第三方账号快速登录";
    label.textColor=[UIColor grayColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    
    UIImageView *line3 = [self createImageViewFrame:CGRectMake(2, labelY + 10, 100, 1) imageName:nil color:[UIColor lightGrayColor]];
    UIImageView *line4 = [self createImageViewFrame:CGRectMake(self.view.frame.size.width-100-4, labelY + 10, 100, 1) imageName:nil color:[UIColor lightGrayColor]];
    
    [_scrollView addSubview:line3];
    [_scrollView addSubview:line4];
    
    
    CGFloat otherY =  CGRectGetMaxY(label.frame) + 20;
    //微信
    weixinBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 50)/2,otherY, 50, 50)];
    //weixinBtn.tag = UMSocialSnsTypeWechatSession;
    weixinBtn.layer.cornerRadius=25;
    weixinBtn=[self createButtonFrame:weixinBtn.frame backImageName:@"wxloginicon" title:nil titleColor:nil font:nil target:self action:@selector(onClickWX:)];
    //qq
    QQBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2 - 100, otherY, 50, 50)];
    //QQBtn.tag = UMSocialSnsTypeMobileQQ;
    QQBtn.layer.cornerRadius=25;
    QQBtn=[self createButtonFrame:QQBtn.frame backImageName:@"qqloginicon" title:nil titleColor:nil font:nil target:self action:@selector(onClickQQ:)];
    
    //新浪微博
    xinlangBtn=[[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2 + 100, otherY, 50, 50)];
    //xinlangBtn.tag = UMSocialSnsTypeSina;
    xinlangBtn.layer.cornerRadius=25;
    xinlangBtn=[self createButtonFrame:xinlangBtn.frame backImageName:@"wbLoginicon" title:nil titleColor:nil font:nil target:self action:@selector(onClickSina:)];
    
    
    [_scrollView addSubview:label];
    [_scrollView addSubview:weixinBtn];
    [_scrollView addSubview:QQBtn];
    [_scrollView addSubview:xinlangBtn];
    [_scrollView addSubview:landBtn];
    [_scrollView addSubview:newUserBtn];
    [_scrollView addSubview:forgotPwdBtn];
}


- (void)onClickQQ:(UIButton *)button
{
    
}

- (void)onClickWX:(UIButton *)button
{
    
}
- (void)onClickSina:(UIButton *)button
{
    
}


-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder {
    UITextField *textField=[[UITextField alloc]initWithFrame:frame];
    
    textField.font=font;
    
    textField.textColor=[UIColor grayColor];
    
    textField.borderStyle=UITextBorderStyleNone;
    
    textField.placeholder=placeholder;
    
    return textField;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color {
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor = color;
    }
    
    return imageView;
}

-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}


//登录
-(void)landClick
{
    if ([userPhone.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入用户名"];
        return;
    }else if ([pwd.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入密码"];
        return;
    }else if (pwd.text.length <6)
    {
        [SVProgressHUD showInfoWithStatus:@"亲,密码长度至少六位"];
        return;
    }
    [SVProgressHUD showWithStatus:@"登录中..."];
    //登录 
    [sharedWebService loginWithAccount:userPhone.text passWord:pwd.text completion:^(BmobUser *user, BOOL succeeded, NSError *error) {
        if (succeeded) {
            ANLog(@"用户:%@登录成功!",user.username);
            [self upUserData];
        } else {
            ANLog(@"登录失败:%@",error);
        }
    }];
}
- (void)upUserData {
    [sharedWebService fetchUserWithBmobCompletion:^(ANBmobUser *user, NSError *error) {
        if (!error) {
            [SVProgressHUD dismiss];
            if (_loginComplete) self.loginComplete();
            //跳转界面
            //[self.navigationController popToRootViewControllerAnimated:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:ANLoginOKNote object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshShoppingCar" object:nil];
            [sharedWebService fetchUserBasicInfoWithCompletion:nil];
        }
    }];
}
//注册
-(void)logon
{
    [self.navigationController pushViewController:[[ANUserLogonViewController alloc]init] animated:YES];
}
//取消
- (void)quit {
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)registration:(UIButton *)button
{
    [self.navigationController pushViewController:[[ANUserQuickLogonViewController alloc]init] animated:YES];
}

-(void)fogetPwd:(UIButton *)button
{
    [self.navigationController pushViewController:[[ANUserForgetPassWordViewController alloc]init] animated:YES];
}

- (void)dealloc {
    ANLog(@"%s",__func__);
}


@end
